package com.aiserver.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 * 提供Redis缓存操作的封装
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 设置缓存
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置缓存并指定过期时间
     */
    public void set(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    /**
     * 获取缓存
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 获取字符串缓存
     */
    public String getString(String key) {
        Object value = get(key);
        return value != null ? value.toString() : null;
    }

    /**
     * 删除缓存
     */
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    /**
     * 检查key是否存在
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 设置过期时间
     */
    public Boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    /**
     * 获取过期时间
     */
    public Long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    /**
     * 递增
     */
    public Long increment(String key) {
        return redisTemplate.opsForValue().increment(key);
    }

    /**
     * 递增指定值
     */
    public Long increment(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * 递减
     */
    public Long decrement(String key) {
        return redisTemplate.opsForValue().decrement(key);
    }

    /**
     * 递减指定值
     */
    public Long decrement(String key, long delta) {
        return redisTemplate.opsForValue().decrement(key, delta);
    }

    // 验证码相关操作
    
    /**
     * 存储验证码
     */
    public void setCaptcha(String key, String code, long timeout) {
        set("captcha:" + key, code, timeout, TimeUnit.SECONDS);
    }

    /**
     * 获取验证码
     */
    public String getCaptcha(String key) {
        return getString("captcha:" + key);
    }

    /**
     * 删除验证码
     */
    public void deleteCaptcha(String key) {
        delete("captcha:" + key);
    }

    // 登录失败次数相关操作
    
    /**
     * 增加登录失败次数
     */
    public Long incrementLoginFailCount(String username) {
        String key = "login_fail:" + username;
        Long count = increment(key);
        // 设置过期时间为30分钟
        expire(key, 30, TimeUnit.MINUTES);
        return count;
    }

    /**
     * 获取登录失败次数
     */
    public Long getLoginFailCount(String username) {
        String key = "login_fail:" + username;
        Object count = get(key);
        return count != null ? Long.valueOf(count.toString()) : 0L;
    }

    /**
     * 清除登录失败次数
     */
    public void clearLoginFailCount(String username) {
        delete("login_fail:" + username);
    }

    // Token黑名单相关操作
    
    /**
     * 将token加入黑名单
     */
    public void addTokenToBlacklist(String token, long expireTime) {
        set("blacklist:" + token, "1", expireTime, TimeUnit.MILLISECONDS);
    }

    /**
     * 检查token是否在黑名单中
     */
    public Boolean isTokenInBlacklist(String token) {
        return hasKey("blacklist:" + token);
    }
}